# -*- coding:utf-8 -*-

import sys
import time

import pygame
from ba4988 import BA4988


def update(ba4988):
    striped = ba4988.display
    screen = pygame.display.set_mode(striped.shape)
    clock = pygame.time.Clock()
    while 1:
        for event in pygame.event.get():
            if event.type == pygame.KEYUP:
                if event.key ==pygame.K_ESCAPE:
                    return
                elif event.key == pygame.K_UP:
                    ba4988.WrMem(0x024E, 0x35+0x80)  # _KEYCODE
                elif event.key == pygame.K_DOWN:
                    ba4988.WrMem(0x024E, 0x38+0x80)  # _KEYCODE
                elif event.key == pygame.K_LEFT:
                    ba4988.WrMem(0x024E, 0x37+0x80)  # _KEYCODE
                elif event.key == pygame.K_RIGHT:
                    ba4988.WrMem(0x024E, 0x39+0x80)  # _KEYCODE
                elif event.key == pygame.K_PAGEUP:
                    ba4988.WrMem(0x024E, 0x3A+0x80)  # _KEYCODE
                elif event.key == pygame.K_PAGEDOWN:
                    ba4988.WrMem(0x024E, 0xB+0x80)  # _KEYCODE
                elif event.key == pygame.K_RETURN:
                    ba4988.WrMem(0x024E, 0x2F+0x80)  # _KEYCODE
                elif event.key == pygame.K_SPACE:
                    ba4988.WrMem(0x024E, 0x36+0x80)  # _KEYCODE
                elif event.key == pygame.K_BREAK:
                    ba4988.WrMem(0x024E, 0x2C+0x80)  # _KEYCODE
                elif event.key == pygame.K_1:
                    ba4988.WrMem(0x024E, 0x08+0x80)  # _KEYCODE
                elif event.key == pygame.K_2:
                    ba4988.WrMem(0x024E, 0x09+0x80)  # _KEYCODE
                elif event.key == pygame.K_3:
                    ba4988.WrMem(0x024E, 0x0A+0x80)  # _KEYCODE
                elif event.key == pygame.K_4:
                    ba4988.WrMem(0x024E, 0x0B+0x80)  # _KEYCODE
                elif event.key == pygame.K_5:
                    ba4988.WrMem(0x024E, 0x0C+0x80)  # _KEYCODE
                elif event.key == pygame.K_6:
                    ba4988.WrMem(0x024E, 0x0D+0x80)  # _KEYCODE
                elif event.key == pygame.K_7:
                    ba4988.WrMem(0x024E, 0x0E+0x80)  # _KEYCODE
                elif event.key == pygame.K_8:
                    ba4988.WrMem(0x024E, 0x0F+0x80)  # _KEYCODE
                elif event.key == pygame.K_9:
                    ba4988.WrMem(0x024E, 0x30+0x80)  # _KEYCODE
                elif event.key == pygame.K_0:
                    ba4988.WrMem(0x024E, 0x31+0x80)  # _KEYCODE
                else:
                    print('event.key:', event.key)
                    continue
                ba4988.setIRQ(0x0308)
            elif event.type == pygame.QUIT:
                return
        pygame.surfarray.blit_array(screen, striped)
        clock.tick(20)
        pygame.display.update()


def main():
    pygame.init()
    pygame.display.set_caption('BA4988')

    ba4988 = BA4988()
    # ba4988.x6502.PC = 0x852A  # SysPutPixel
    # ba4988.x6502.A = 8  # x
    # ba4988.memory[0x0C20] = 0x00
    # ba4988.flash[0x17AF] = 0  # y
    # ba4988.flash[0x17B0] = 1  # data
    ba4988.start()
    update(ba4988)
    ba4988.stop()

    # alldone
    pygame.quit()


if __name__ == '__main__':
    main()
